import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_upgrade_plugin/flutter_upgrade_plugin.dart';

class TestDemo extends StatefulWidget {
  const TestDemo({Key? key}) : super(key: key);

  @override
  State<TestDemo> createState() => _TestDemoState();
}

class _TestDemoState extends State<TestDemo> {
  String _platformVersion = 'Unknown';
  @override
  void initState() {
    super.initState();
    initPlatformState();
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    String platformVersion;
    // Platform messages may fail, so we use a try/catch PlatformException.
    // We also handle the message potentially returning null.
    try {
      platformVersion = await FlutterUpgrade.getPlatformVersion() ??
          'Unknown platform version';
    } on PlatformException {
      platformVersion = 'Failed to get platform version.';
    }

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

    setState(() {
      _platformVersion = platformVersion;
    });
  }

  Future<AppUpgradeInfo> _checkAppInfo() async {
    //这里一般访问网络接口，将返回的数据解析成如下格式
    return Future.delayed(Duration(seconds: 1), () async {
      var appinfo = await FlutterUpgrade.getAppInfo();
      return AppUpgradeInfo(
        title: '新版本v${appinfo.versionCode}',
        apkDownloadUrl:
            'http://misdev.stag.esharex.com/static/upload/oa/apk/2022-11-03/1587992833248247809.apk',
        contents: """
          '1、支持立体声蓝牙耳机，同时改善配对性能<br/>',
          '2、提供屏幕虚拟键盘<br/>\n',
          '3、更简洁更流畅，使用起来更快<br/>\n',
          '4、修复一些软件在使用时自动退出bug<br/>\n',
          '5、新增加了分类查看功能<br/>\n'
             '1、支持立体声蓝牙耳机，同时改善配对性能<br/>\n',
          '2、提供屏幕虚拟键盘<br/>\n',
          '3、更简洁更流畅，使用起来更快<br/>\n',
          '4、修复一些软件在使用时自动退出bug<br/>\n',
          '5、新增加了分类查看功能<br/>\n'
             '1、支持立体声蓝牙耳机，同时改善配对性能<br/>\n',
          '2、提供屏幕虚拟键盘<br/>\n',
          '3、更简洁更流畅，使用起来更快<br/>\n',
          '4、修复一些软件在使用时自动退出bug<br/>\n',
          '5、新增加了分类查看功能<br/>\n'
        ]""",
        force: false,
      );
    });
  }

  _dialogShow() {
    AppUpgrade.appUpgrade(
      context,
      _checkAppInfo(),
      cancelText: '稍后再说',
      okText: '立即更新',
      iosAppId: 'id88888888',
      appMarketInfo: AppMarket.huaWei,
      progressStyle: ProgressStyle.linear,
      downloadProgress: (count, total) {
        print('count:$count,total:$total');
      },
      downloadStatusChange: (DownloadStatus status, {dynamic error}) {
        print('status:$status,error:$error');
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Plugin example app'),
      ),
      body: Column(
        children: [
          Center(
            child: Text('Running on11: $_platformVersion\n'),
          ),
          ElevatedButton(
            onPressed: () async {
              // var info= await _flutterUpgradePlugin.installAppForAndroid('http://misdev.stag.esharex.com/static/upload/oa/apk/2022-11-03/1587992833248247809.apk');
              var info = await FlutterUpgrade.getAppInfo();
              print(info.packageName);
            },
            child: Text('获取版本'),
          ),

          ///如果手机上安装多个应用市场则弹出对话框，由用户选择进入哪个市场
          ElevatedButton(
            onPressed: () async {
              // var info= await _flutterUpgradePlugin.installAppForAndroid('http://misdev.stag.esharex.com/static/upload/oa/apk/2022-11-03/1587992833248247809.apk');
              var info = await FlutterUpgrade.toMarket();
              print(info.packageName);
            },
            child: Text('跳转到应用商店'),
          ),
          ElevatedButton(
            onPressed: () async {
              _dialogShow();
            },
            child: Text('更新'),
          )
        ],
      ),
    );
  }
}
